package com.mantisframework.sys.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import jodd.util.StringUtil;

import org.springframework.stereotype.Service;

import com.mantisframework.core.base.service.BaseService;
import com.mantisframework.core.page.PageBean;
import com.mantisframework.model.MSysUsers;
import com.mantisframework.sys.dao.ISysUsersDao;
import com.mantisframework.sys.enumbean.UserSearchFields;
import com.mantisframework.sys.service.ISysUsersService;

@Service
public class SysUsersService extends BaseService<MSysUsers> implements
		ISysUsersService {
	@Resource
	private ISysUsersService sysUsersService;
	@Resource
	private ISysUsersDao sysUsersDao;
	
	@Override
	public MSysUsers getPojoById(Serializable id) {
		return sysUsersDao.getById(MSysUsers.class, id);
	}

	public boolean modifyUser(MSysUsers user) {
		return sysUsersDao.modify(user);
	}
	
	public boolean deleteUserById(Serializable id) {
		MSysUsers user = this.getPojoById(id);
		return sysUsersDao.delete(user);
	}
	
	public PageBean querySysUsers(Map<UserSearchFields, String> criterias,
			PageBean pagebean) {
		int count = 0;
		StringBuffer buffer = new StringBuffer(" ");
		for (UserSearchFields field : UserSearchFields.values()) {
			switch (field) {
			case userName:
				if (criterias.containsKey(field)) {
					buffer.append(count == 0 ? " where" : " and")
							.append(" userName like '%")
							.append(criterias.get(field)).append("'%");
					count++;
				}
				break;
			case loginName:
				if (criterias.containsKey(field)) {
					buffer.append(count == 0 ? " where" : " and")
							.append(" loginName like '%")
							.append(criterias.get(field)).append("%'");
					count++;
				}
				break;
			case isLock:
				if (criterias.containsKey(field)) {
					buffer.append(count == 0 ? " where" : " and")
							.append(" isLock = '").append(criterias.get(field))
							.append("'");
					count++;
				}
				break;
			default:
				break;
			}
		}
		return sysUsersService.getPojoByCondition(new MSysUsers(),
				buffer.toString(), pagebean);
	}

	public PageBean querySysUsers(Map<UserSearchFields, String> criterias,
			PageBean pagebean,String orderDirect, String orderField) {
		int count = 0;
		StringBuffer buffer = new StringBuffer(" ");
		for (UserSearchFields field : UserSearchFields.values()) {
			switch (field) {
			case userName:
				if (criterias.containsKey(field)) {
					buffer.append(count == 0 ? " where" : " and")
					.append(" userName like '%")
					.append(criterias.get(field)).append("%'");
					count++;
				}
				break;
			case loginName:
				if (criterias.containsKey(field)) {
					buffer.append(count == 0 ? " where" : " and")
					.append(" loginName like '%")
					.append(criterias.get(field)).append("%'");
					count++;
				}
				break;
			case isLock:
				if (criterias.containsKey(field)) {
					buffer.append(count == 0 ? " where" : " and")
					.append(" isLock = '").append(criterias.get(field))
					.append("'");
					count++;
				}
				break;
			default:
				break;
			}
		}
		if(StringUtil.isNotEmpty(orderField)&&StringUtil.isNotBlank(orderField)) {
			buffer.append(" order by ").append(orderField);
			if(StringUtil.isNotEmpty(orderDirect)&&StringUtil.isNotBlank(orderDirect)) {
				buffer.append(" ").append(orderDirect).append(" ");
			} else {
				buffer.append(" asc ");
			}
		} else {
			buffer.append(" order by loginName asc");
		}
		return sysUsersService.getPojoByCondition(new MSysUsers(),
				buffer.toString(), pagebean);
	}
	
	public boolean hasExistUser(String loginName) {
		String condition = " where loginName='" + loginName + "'";
		List<MSysUsers> users = sysUsersService.getPojoAll(new MSysUsers(),
				condition);
		if (null != users && !users.isEmpty() && users.size() > 0)
			return true;
		else
			return false;
	}

	public boolean validateRequired(MSysUsers user, boolean validateLoginName) {
		if(validateLoginName) {
			if(StringUtil.isEmpty(user.getLoginName()) || StringUtil.isBlank(user.getLoginName())) {
				return false;
			}
		}
		if(StringUtil.isEmpty(user.getUserName()) || StringUtil.isBlank(user.getUserName())) {
			return false;
		}
		if(StringUtil.isEmpty(user.getMobilePhone()) || StringUtil.isBlank(user.getMobilePhone())) {
			return false;
		}
		if(StringUtil.isEmpty(user.getEmail()) || StringUtil.isBlank(user.getEmail())) {
			return false;
		}
		
		return true;
	}

	public void setSysUsersService(ISysUsersService sysUsersService) {
		this.sysUsersService = sysUsersService;
	}

	public void setSysUsersDao(ISysUsersDao sysUsersDao) {
		this.sysUsersDao = sysUsersDao;
	}
}
